<template>
  <div class="content" v-loading="submitBtn">
    <div class="scroll-view-box">
      <el-form label-width="120px" :inline="true" :model="formData" :rules="formRules" ref="elFrom"
        label-position="top">
        <el-form-item label="是否需集采订货" required>
          <el-radio-group v-model="formData.is_order_goods">
            <el-radio :label="1">需要</el-radio>
            <el-radio :label="2">不需要</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="订单需求(明细)" prop="order_demand" v-if="formData.is_order_goods == 1">
          <el-input v-model="formData.order_demand" placeholder="请输入订单需求(明细)" clearable class="input-width"></el-input>
        </el-form-item>
        <el-form-item label="工长" prop="foreman_idx">
          <el-select v-model="formData.foreman_idx" placeholder="请选择工长" filterable clearable class="input-width">
            <el-option v-for="(item, idx) in foremanList" :key="idx" :label="item.name" :value="idx">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="现场核实照片" prop="site_file">
          <div class="file-list-box">
            <div class="file-item-box" v-for="(item, index) in formData.site_file" :key="index">
              <div class="el-icon-delete-box" @click="delFileFunc(index)">
                <i class="el-icon-error"></i>
              </div>
              <el-tooltip class="item" effect="dark" :content="item.name" placement="top">
                <img class="img" :src="$fileImgFunc(item)" />
              </el-tooltip>
            </div>
            <el-upload multiple class="avatar-uploader" action="#" accept="image/*,video/*,.m4a,.mp3,.zip,.rar"
              :show-file-list="false" :auto-upload="false" list-type="picture-card" :on-change="changeFile">
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveSubmit" :loading="submitBtn">提交</el-button>
        <el-button @click="cancelFunc">关闭</el-button>
      </div>
    </div>
  </div>
</template>
<script>
import afterSalesApi from '@/api/afterSalesApi'
import foremanApi from '@/api/foremanApi';

export default {
  name: 'siteFeedback',
  data() {
    let validateFileList = (rule, value, callback) => {
      console.log(rule, value)
      if (!value.length) {
        callback(new Error('请选择附件'));
      } else {
        callback();
      }
    };
    return {
      loading: false,
      formData: {
        id: '',
        is_order_goods: 1,
        order_demand: '',
        foreman_idx: '',
        site_file: []
      },
      formRules: {
        site_file: [{ required: true, validator: validateFileList, trigger: "change" }],
        order_demand: [{ required: true, message: "请输入订单需求(明细)", trigger: "change" }],
      },
      submitBtn: false,
      foremanList: [],
    };
  },
  computed: {
  },
  mounted() {
    let id = this.$route.query.sn
    if (id) {
      this.formData.id = id
    }
    this.getList()
  },
  methods: {
    // 选择文件
    changeFile(file) {
      this.formData.site_file.push(file)
    },
    delFileFunc(index) {
      this.formData.site_file.splice(index, 1);
    },
    // 提交
    saveSubmit() {
      this.$refs['elFrom'].validate((valid) => {
        if (valid) {
          let formData = this.formData
          this.submitBtn = true;
          let obj = {
            after_sales_id: formData.id,
            is_order_goods: formData.is_order_goods,
            order_demand: formData.order_demand,
            foreman_id: formData.foreman_idx.toString() ? this.foremanList[formData.foreman_idx].id : '',
            foreman_name: formData.foreman_idx.toString() ? this.foremanList[formData.foreman_idx].name : '',
          }
          let formdata = new FormData();
          formdata.append('data', JSON.stringify(obj))
          formData.site_file.forEach(item => {
            formdata.append('site_file[]', item.raw);
          })
          afterSalesApi.feedbackSiteSituation(formdata).then(res => {
            console.log(res, '提交成功')
            if (res.code == 200) {
              this.$message({
                message: "提交成功",
                type: "success",
              });
              this.cancelFunc()
            } else {
              this.$message.error(res.message);
            }
            this.submitBtn = false;
          })
        } else {
          return false;
        }
      });
    },
    // 取消
    cancelFunc() {
      this.$goBackTab(this);
    },

    // 获取列表数据
    getList() {
      foremanApi.getForemanList({
        page: 1,
        num: 999,
        show_deactivate: 2,
      }).then(res => {
        console.log(res, "获取工长")
        if (res.code == 200) {
          this.foremanList = res.data.list;
        }
      })
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/addPage.scss";
</style>